#region License

/*
  GShield - Game Shield
  Copyright (C) 2006-2007 WaveTech Digital Technologies, Inc.
                                http://www.wtdt.com

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#endregion License

#region Includes

using System;
using System.Collections.Generic;
using System.Linq;

#endregion

namespace WaveTech.GShield.Data
{
	public static class DirectoryNameExclusionsDAO
	{
		#region Get All Directory Name Exclusions

		public static List<string> GetAllDirectoryNameExclusions()
		{
			var exclusionNames = from de in Database.DC.DirectoryNameExclusions
			                     select de.Name;

			return exclusionNames.ToList();
		}

		#endregion Get All Directory Name Exclusions

		public static DirectoryNameExclusion GetExclusionByName(string name)
		{
			var exclusion = from e in Database.DC.DirectoryNameExclusions
			                where e.Name == name
			                select e;

			return exclusion.Single();
		}

		public static void Delete(string name)
		{
			var exclusion = from e in Database.DC.DirectoryNameExclusions
			                where e.Name == name
			                select e;

			Database.DC.DirectoryNameExclusions.DeleteAllOnSubmit(exclusion);
			Database.DC.SubmitChanges();
		}

		public static void Insert(string name)
		{
			DirectoryNameExclusion e = new DirectoryNameExclusion();
			e.Name = name;
			e.DateAdded = DateTime.Now;

			Database.DC.DirectoryNameExclusions.InsertOnSubmit(e);
			Database.DC.SubmitChanges();
		}
	}

	#region Old DirectoryNameExclusionsDAO

	/*
	public class DirectoryNameExclusionsDAO
	{
		#region Get Exclusion Names
		/// <summary>
		/// Returns all the directory name exclusions names from the database.
		/// </summary>
		/// <returns>ArrayList of directory name exclustions as strings.</returns>
		public ArrayList GetExclusionNames()
		{
			ArrayList l = new ArrayList();
			Database db;
			DbCommand dbCommand;

			db = DatabaseFactory.CreateDatabase();
			dbCommand = db.GetSqlStringCommand("SELECT [Name] FROM DirectoryNameExclusions");

			IDataReader r = db.ExecuteReader(dbCommand);

			while (r.Read())
			{
				l.Add(Encryption.Decrypt(r[0].ToString()));
			}

			return l;
		}
		#endregion Get Exclusion Names

		#region Get Exclusions
		/// <summary>
		/// Returns all the directory name exclusions names and their ID's from the database.
		/// </summary>
		/// <returns>SortedList of  directory name exclusions names and their ID's</returns>
		public SortedList GetExclusions()
		{
			SortedList l = new SortedList();
			Database db;
			DbCommand dbCommand;

			db = DatabaseFactory.CreateDatabase();
			dbCommand = db.GetSqlStringCommand("SELECT [Id], [Name] FROM DirectoryNameExclusions");

			IDataReader r = db.ExecuteReader(dbCommand);

			while (r.Read())
			{
				l.Add(r[0], Encryption.Decrypt(r[1].ToString()));
			}

			return l;
		}
		#endregion Get Exclusions

		#region Get Exclusion By Name
		/// <summary>
		/// Returns the directory name exclusion Id by it's name from the database.
		/// </summary>
		/// <param name="name">Name of the Directory Name Exclusion</param>
		/// <returns>Id of  directory name exclusion if found, else null</returns>
		public int? GetExclusionByName(string name)
		{
			Object retVal;
			Database db;
			DbCommand dbCommand;

			db = DatabaseFactory.CreateDatabase();
			dbCommand = db.GetSqlStringCommand("SELECT [Id] FROM DirectoryNameExclusions WHERE ([Name] = @Name)");
			db.AddInParameter(dbCommand, "@Name", DbType.String, Encryption.Encrypt(name));

			retVal = db.ExecuteScalar(dbCommand);

			if (retVal != null)
				return (int) retVal;
			else
				return null;
		}
		#endregion Get Exclusion By Name

		#region Delete
		/// <summary>
		/// Removes a Directory Name Exclusion from the database by it's ID.
		/// </summary>
		/// <param name="id">Identifier of the Exclusion to delete.</param>
		public void Delete(int? id)
		{
			if (id == null)
				throw new ArgumentNullException("id");

			Database db;
			DbCommand dbCommand;

			db = DatabaseFactory.CreateDatabase();
			dbCommand = db.GetSqlStringCommand("DELETE FROM DirectoryNameExclusions WHERE ([Id] = @Id)");

			db.AddInParameter(dbCommand, "@Id", DbType.Int32, id);

			db.ExecuteNonQuery(dbCommand);
		}

		/// <summary>
		/// Removes a Directory Name Exclusion from the database by it's Name.
		/// </summary>
		/// <param name="name">Name of the Exclusion to delete.</param>
		public void Delete(string name)
		{
			if (name == null || name.Length <= 0)
				throw new ArgumentNullException("name");

			Database db;
			DbCommand dbCommand;

			db = DatabaseFactory.CreateDatabase();
			dbCommand = db.GetSqlStringCommand("DELETE FROM DirectoryNameExclusions WHERE ([Name] = @Name)");

			db.AddInParameter(dbCommand, "@Name", DbType.String, Encryption.Encrypt(name));

			db.ExecuteNonQuery(dbCommand);
		}
		#endregion Delete

		#region Insert
		/// <summary>
		/// Inserts a new Directory Name Exclusion into the database
		/// </summary>
		/// <param name="name">Name of the Exclusion to add</param>
		/// <returns>The Id number of the newly Inserted Exclusion</returns>
		public int? Insert(string name)
		{
			if (name == null || name.Length <= 0)
				throw new ArgumentNullException("name");

			Database db;
			DbCommand dbCommand;
			int? _Id;

			db = DatabaseFactory.CreateDatabase();
      dbCommand = db.GetSqlStringCommand("INSERT INTO [DirectoryNameExclusions] ([Name]) VALUES (@Name) SELECT SCOPE_IDENTITY()");

			db.AddInParameter(dbCommand, "@Name", DbType.String, Encryption.Encrypt(name));

      _Id = int.Parse(db.ExecuteScalar(dbCommand).ToString());

			return _Id;
		}
		#endregion Insert
	}
    */

	#endregion Old DirectoryNameExclusionsDAO
}